var sliderLoaded = false;

GoUni.controller('UniversityCtrl', function ($scope, DataService) {
	
	$scope.translations = {
	};
	
	$scope.filters = {
		id:'',
		page:1,
	};

    $scope.init = function (university_id) {
    	$scope.filters.id = university_id;
    	$scope.search();
    };
	
	$scope.search = function() {
	
		var none_empty_filters = _.object(
			_.filter(
				_.pairs($scope.filters), 
				function(filter){
					return (filter[1]!='') && (filter[1]!=0);
				}
			)
		);
		
		var university_url = '/api/university/?'+ encode_query_data(none_empty_filters) ;
		DataService.get(university_url).then(function (data) {
			$scope.university = data.university;
			$scope.courses = data.courses.items;
			$scope.total_items = data.courses.total_items;
			$scope.items_per_page = data.courses.items_per_page;

			$scope.images = data.images;
		}, function(status){
			console.log(status);
		});

	};
	
	$scope.page_changed = function() {
		$scope.search();
	};
	
})
.directive('postRepeatDirective', function () {
    return function (scope, element, attrs) {
        if (scope.$last && !sliderLoaded) {
            $("#owl-popular-posts").owlCarousel({
                autoPlay: 5000,
                stopOnHover: true,
                navigation: true,
                pagination: true,
                rewindNav: true,
                items: 5,
                navigationText: ["<i class='icon-left-open-mini'></i>", "<i class='icon-right-open-mini'></i>"]
            });
            sliderLoaded = true;
        }
    };
});


